<template>
  <div class="container">
    <div class="export-header">
      <el-button
        icon="el-icon-edit"
        type="primary"
        size='medium'
        @click="addItem()"
      >新增轮播图</el-button>
    </div>
    <div class="table_con">
      <el-table
        v-loading="loading"
        :data="tableData"
        :border='true'
      >
        <el-table-column
          :formatter="formatter"
          prop="num"
          label="序号"
        ></el-table-column>
        <el-table-column
          :formatter="formatter"
          prop="avatarUrl "
          label="轮播图"
        >
          <template slot-scope="scope">
            <img
              style="width:150px;height:100px;"
              :src="scope.row.coverUrl"
              alt=""
            >
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          label="操作"
          width='220'
        >
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              type="primary"
              size="mini"
              @click="addItem(scope.row.id)"
            >编辑</el-button>
            <el-button
              icon="el-icon-delete"
              type="danger"
              size="mini" @click="handleDel(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageIndex"
        :page-size="pageSize"
        :page-sizes='[10,20,30,40]'
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageCount"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
let CONSTANT = require("../../constant/constant.js");
let common = require("../../common.js");
export default {
  data() {
    return {
      tableData: [],
      pageCount: null,
      pageIndex: 1,
      pageSize: 10,
      loading: true,
      mobile: null,
      nickname: null
    };
  },
  mounted() {
    this.pageIndex = this.$router.currentRoute.query.pageIndex
      ? this.$router.currentRoute.query.pageIndex
      : 1;
    this.getList();
  },
  methods: {
    addItem(id) {
      this.$router.push({
        path: "/bannerEdit",
        query: {
          id: id||''
        }
      });
    },
    handleDel(id) {
        this.$confirm("确定删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let url = CONSTANT.URL.LIST.bannerDelete;
            common.del(url, id, res => {
              if (res.status == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.getList();
              } else {
                this.$message({
                  type: "warning",
                  message: res.data.errMsg
                });
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },
    formatter(row, column, cellValue) {
      return cellValue ? cellValue : "- -";
    },
    getList: function() {
      let url = CONSTANT.URL.LIST.bannerPage;
      let data = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      };
      this.loading = true;
      common.requestAjax(url, JSON.stringify(data), null, res => {
        this.loading = false;
        let data = res.data;
        data.bussData.forEach((item,index)=>{
          item.num = 10*(this.pageIndex-1)+index+1
        })
        this.tableData = data.bussData;
        this.pageCount = data.count;
      });
    },
    search: function(e) {
      if (e.keyCode == 13) {
        this.pageIndex = 1;
        this.getList();
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getList();
    }
  }
};
</script>
